﻿using Little.Soldier.SWIFT;
using System.IO;
using System.Text;

namespace Little.Soldier
{
  internal class IWriter
  {
    private bool U0002 = true;
    private const string _Terminator = "\r\n";
    private readonly TextWriter _TextWriter;

    internal IWriter(TextWriter writer)
    {
      this._TextWriter = writer;
    }

    private void SetTag(SwiftTag t)
    {
      string str1;
      switch (t.Name)
      {
        case null:
          str1 = "";
          break;
        default:
          str1 = t.Name;
          break;
      }
      string str2 = str1;
      string str3 = t.Value == null ? "" : t.Value;
      if (!(str2 != "") && !(str3 != ""))
        return;
      if (str2 != "")
          this.TextBlock(((object)new StringBuilder().Append("{").Append(str2).Append(":").Append(str3)).ToString());
      else
          this.TextBlock(((object)new StringBuilder().Append("{").Append(str3)).ToString());
      if (t.UnparsedTextsSize > 0)
        this.TextBlock(t.UnparsedTexts);
      this.TextBlock("}");
    }

    private void U0090U0002(SwiftTag t)
    {
      string str1;
      switch (t.Name)
      {
        case null:
          str1 = "";
          break;
        default:
          str1 = t.Name;
          break;
      }
      string str2 = str1;
      string str3 = t.Value == null ? "" : t.Value;
      if (str2 != "")
        this.TextBlock(((object) new StringBuilder().Append(":").Append(str2).Append(":").Append(str3).Append("\r\n")).ToString());
      if (t.UnparsedTextsSize <= 0)
        return;
      this.TextBlock(t.UnparsedTexts);
    }

    internal virtual void CloseBlock1(SwiftBlock1 b)
    {
      if (b.UnparsedTextsSize > 0)
        goto label_2;
label_1:
      this.TextBlock("}");
      return;
label_2:
      this.TextBlock(b.UnparsedTexts);
      goto label_1;
    }

    internal virtual void CloseBlock2(SwiftBlock2 b)
    {
      if (b.UnparsedTextsSize > 0)
        goto label_2;
label_1:
      this.TextBlock("}");
      return;
label_2:
      this.TextBlock(b.UnparsedTexts);
      goto label_1;
    }

    internal virtual void CloseBlock3(SwiftBlock3 b)
    {
      if (b.UnparsedTextsSize > 0)
        goto label_2;
label_1:
      this.TextBlock("}");
      return;
label_2:
      this.TextBlock(b.UnparsedTexts);
      goto label_1;
    }

    internal virtual void CloseBlock4(SwiftBlock4 b)
    {
      if (b.UnparsedTextsSize > 0)
        goto label_2;
label_1:
      this.TextBlock(((object) new StringBuilder().Append(!this.U0002 ? "" : "-").Append("}")).ToString());
      return;
label_2:
      this.TextBlock(b.UnparsedTexts);
      goto label_1;
    }

    internal virtual void CloseBlock5(SwiftBlock5 b)
    {
      if (b.UnparsedTextsSize > 0)
        goto label_2;
label_1:
      this.TextBlock("}");
      return;
label_2:
      this.TextBlock(b.UnparsedTexts);
      goto label_1;
    }

    internal virtual void U0096U0002(SwiftBlockUser b)
    {
      if (b.UnparsedTextsSize > 0)
        goto label_2;
label_1:
      this.TextBlock("}");
      return;
label_2:
      this.TextBlock(b.UnparsedTexts);
      goto label_1;
    }

    internal virtual void U0097U0002(SwiftMessage m)
    {
      if (m.UnparsedTextsSize > 0)
        goto label_2;
label_1:
      this.U0002 = true;
      return;
label_2:
      this.TextBlock(m.UnparsedTexts);
      goto label_1;
    }

    internal virtual string GetTagValue(SwiftTag t, int block)
    {
      string str = t.Value;
      if (t.Name == null && str.StartsWith(((object) new StringBuilder().Append(block).Append(":")).ToString()) && str.Length > 2)
        return str.Substring(2);
      else
        return str;
    }

    internal virtual void SetBlock1(SwiftBlock1 b)
    {
        this.TextBlock("{1:");
    }

    internal virtual void SetBlock2(SwiftBlock2 b)
    {
      this.TextBlock("{2:");
    }

    internal virtual void SetBlock3(SwiftBlock3 b)
    {
        this.TextBlock("{3:");
    }

    internal virtual void SetBlock4(SwiftBlock4 b)
    {
      this.TextBlock(((object) new StringBuilder().Append("{4:").Append(!this.U0002 ? "" : "\r\n")).ToString());
    }

    internal virtual void SetBlock5(SwiftBlock5 b)
    {
      this.TextBlock("{5:");
    }

    internal virtual void U009DU0002(SwiftBlockUser b)
    {
      this.TextBlock(((object) new StringBuilder().Append("{").Append(b.Name).Append(":")).ToString());
    }

    internal virtual void U009EU0002(SwiftMessage m)
    {
      this.U0002 = true;
      SwiftBlock1 swiftBlock1 = m == null ? (SwiftBlock1) null : m.Block1;
      if (swiftBlock1 != null)
      {
        string str1 = swiftBlock1.ApplicationIdentifier == null ? "" : swiftBlock1.ApplicationIdentifier;
        string str2 = swiftBlock1.ServiceIdentifier == null ? "" : swiftBlock1.ServiceIdentifier;
        if (swiftBlock1.GetValue() == null || swiftBlock1.GetValue() == "")
          this.U0002 = true;
        else if (str1 != "F" || str2 != "01")
          this.U0002 = false;
        else
          this.U0002 = true;
      }
      else
      {
        SwiftBlock2 swiftBlock2 = m == null ? (SwiftBlock2) null : m.Block2;
        if (swiftBlock2 != null)
        {
          if ((swiftBlock2.MessageType == null ? "" : swiftBlock2.MessageType).StartsWith("0"))
            this.U0002 = false;
          else
            this.U0002 = true;
        }
        else
          this.U0002 = true;
      }
    }

    internal virtual void U009FU0002(SwiftBlockBase b, SwiftTag t)
    {
      if (b == null)
        return;
      if (b is SwiftBlock3)
        goto label_10;
label_3:
      if (b is SwiftBlock4)
        this.U009FU0002((SwiftBlock4) b, t);
      if (b is SwiftBlock5)
        this.U009FU0002((SwiftBlock5) b, t);
      if (!(b is SwiftBlockUser))
        return;
      this.U009FU0002((SwiftBlockUser) b, t);
      return;
label_10:
      this.U009FU0002((SwiftBlock3) b, t);
      goto label_3;
    }

    internal virtual void U009FU0002(SwiftBlock3 b, SwiftTag t)
    {
      this.SetTag(t);
    }

    internal virtual void U009FU0002(SwiftBlock4 b, SwiftTag t)
    {
      if (!this.U0002)
        this.SetTag(t);
      else
        this.U0090U0002(t);
    }

    internal virtual void U009FU0002(SwiftBlock5 b, SwiftTag t)
    {
      this.SetTag(t);
    }

    internal virtual void U009FU0002(SwiftBlockUser b, SwiftTag t)
    {
      this.SetTag(t);
    }

    internal virtual void SetBlock1(SwiftBlock1 b, string v)
    {
      switch (v)
      {
        case null:
          break;
        default:
          if (!(v != ""))
            break;
          this.TextBlock(v);
          break;
      }
    }

    internal virtual void SetBlock2(SwiftBlock2 b, string v)
    {
      switch (v)
      {
        case null:
          break;
        default:
          if (!(v != ""))
            break;
          this.TextBlock(v);
          break;
      }
    }

    private void TextBlock(UnparsedTextList texts)
    {
      if (texts.Size <= 0)
        return;
      for (int index = 0; index < texts.Size; ++index)
      {
        if (texts.U0084U0003(index))
            this.TextBlock(texts.U0082U0003(index));
      }
    }

    private void TextBlock(string s)
    {
      try
      {
        this._TextWriter.Write(s);
      }
      catch (IOException ex)
      {
        throw ex;
      }
    }
  }
}
